﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SchoolCardSystem
{
    public partial class UserInf : System.Web.UI.Page
    {
        //设置卡号和填充的用户名
        String userId;
        String afterUser;
        String getUser;
        protected void Page_Load(object sender, EventArgs e)
        {
            //定义数据库信息
            MySqlConnection connection = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
            //打开数据库
            connection.Open();
            userId = Session["name"].ToString();
            cardId.Text = userId;
            string sqlstr = @"select userName from user where userId=@userId";
            //Command 查询语句
            MySqlCommand cmd = new MySqlCommand(sqlstr, connection);
            //设置@userId的类型
            cmd.Parameters.Add(new MySqlParameter("@userId", MySqlDbType.VarChar, 11));
            cmd.Parameters["@userId"].Value = userId;

            //在线模式
            MySqlDataReader dr = cmd.ExecuteReader();
            if (!dr.Read())
            {
                errorText.Style["display"] = "inline";
            }
            //查询成功
            else
            {
                BeforeName.Text = dr["userName"].ToString();
                afterUser = BeforeName.Text;
            }
            connection.Close();
        }

        protected void submit_Click(object sender, EventArgs e)
        {
            if (userName.Text.Trim() == "")
            {
                errorText.Style["display"] = "inline";
                errorText.Text = "请输入用户名";
                userName.Focus();
            }
            else if(userName.Text == afterUser){
                errorText.Style["display"] = "inline";
                errorText.Text = "用户名相同";
                userName.Focus();
            }else{
                MySqlConnection connection2 = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
                //打开数据库
                connection2.Open();
                //更新语句
                string sqlstr2 = @"update user set userName=@userName where userId=@userId";
                //Command 更新语句
                MySqlCommand update = new MySqlCommand(sqlstr2, connection2);
                //设置@price/@userId的类型
                update.Parameters.Add(new MySqlParameter("@userName", MySqlDbType.VarChar, 11));
                update.Parameters.Add(new MySqlParameter("@userId", MySqlDbType.VarChar, 11));
                update.Parameters["@userId"].Value = userId;
                //获取更改后的UserName
                getUser = userName.Text;
                update.Parameters["@userName"].Value = getUser;
                errorText.Style["display"] = "inline";
                errorText.Text = "更改成功";
                BeforeName.Text = getUser;
                //执行语句
                update.ExecuteNonQuery();
                connection2.Close();
            }   
        }

        protected void reset_Click(object sender, EventArgs e)
        {
            //取消则跳转回原页面
            Response.Redirect("index.aspx");
        }
    }
}